﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ForgetTraffic.AnsycLibrary;
using ForgetTraffic.DataModel;
using ForgetTraffic.DataTypes;
using ForgetTraffic.DataTypes.Interfaces;
using ForgetTraffic.SapoTraffic;
using ForgetTraffic.TrafficIncidences.SolveIncidents;
using StructureMap;

namespace ForgetTraffic.ParallelsServices
{
   public  class SolveIncidentService : Interfaces.IRunnable<object>
   {

       private static SolveIncidentService _singleton;
 
       public static SolveIncidentService SINGLETON()
       {
           if(_singleton == null)
               _singleton = new SolveIncidentService();

           return _singleton;
       }

        private readonly Object _snyc;

        private readonly Object _solveSnyc;

        private IGenericService<tblIncident> _service;
        private Timer _timer;
        private Timer _timerSapo;

        //private Timer _timer;

       public SolveIncidentService()
       {
           _snyc = new object();
           _solveSnyc = new object();

           _service = ObjectFactory.GetInstance<IGenericService<tblIncident>>();

           _service.Solve(null, null);

           TimeSpan _deplay = new TimeSpan(0,0,20);

           _timer = new Timer(Solve, _snyc, _deplay , Consts.SolveIncidentInterval );
           _timer = new Timer(RunSapoIncidents, _snyc, _deplay ,  Consts.SapoRunnerInterval);

           //With my timer generic
           // _timer = new GenericTimer<object>(_snyc, Consts.SolveIncidentInterval, 200, Solve);
           //_timerSapo = new GenericTimer<object>(_snyc, Consts.SapoRunnerInterval, 200, RunSapoIncidents);

       }

      

       public void Solve( object tInput)
       {
           lock (_solveSnyc)
           {
               _service.Solve(null, tInput);
               ForgetTraffic.InitializeStructure.GeneralInitialize.Commit();
    
           }
       }

       public void RunSapoIncidents( object tInput)
       {
           lock (_snyc)
           {
               SapoToService.ContactToService();
               ForgetTraffic.InitializeStructure.GeneralInitialize.Commit();    
           }
       }

       public void Run(object snyc)
       {
           throw new NotImplementedException();
       }
   }
}
